package com.tyjc.unique;

import android.app.ProgressDialog;
import android.os.Handler;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;

import com.alibaba.fastjson.JSON;
import com.tyjc.unique.bean.SSQTrendChart;
import com.tyjc.unique.net.StringResultBean;
import com.tyjc.unique.net.UrlConstants;
import com.tyjc.lottery.R;
import com.fyzk.utils.LogUtil;

import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Arrays;

import http.Common.CommandUtil;

/**
 * Created by miao on 16/6/12.
 */
@ContentView(R.layout.fg_ssqcoldhot)
public class ColdHotFragment extends _AnalaysesBaseFragment {

    @ViewInject(R.id.cold_30)
    AppCompatTextView cold_30;

    @ViewInject(R.id.cold_50)
    AppCompatTextView cold_50;

    @ViewInject(R.id.cold_100)
    AppCompatTextView cold_100;

    @ViewInject(R.id.hot_30)
    AppCompatTextView hot_30;

    @ViewInject(R.id.hot_50)
    AppCompatTextView hot_50;

    @ViewInject(R.id.hot_100)
    AppCompatTextView hot_100;


    private ArrayList<SSQTrendChart> arrayChartItems;
    ProgressDialog progressDialog;


    long startTime = 0L;
    long endTime = 0L;


    @Override
    public void showData() {
        super.showData();
        progressDialog = new ProgressDialog(getContext(),
                R.style.Custom_Progress);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setMessage("冷热号码分析中...");
        startTime = System.currentTimeMillis();
        progressDialog.show();
        requestChart();
    }

    final long waittime = 2000;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {

        if (resultBean.getStatus()==0 ) {
            endTime = System.currentTimeMillis();
            arrayChartItems = (ArrayList<SSQTrendChart>) JSON.parseArray(
                    resultBean.getData(), SSQTrendChart.class);

            if(endTime - startTime > waittime){
                showColdHotNum();
            }else{
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showColdHotNum();
                    }
                },(waittime-endTime + startTime) );
            }

        }
    }


    private void showColdHotNum(){

        progressDialog.dismiss();

        coldhotCompute(0);
        hot_100.setText(Html.fromHtml("热号：红球 <font color=\"#eb4f38\"><b>" +lowNinePlusZero( min_1_index) + "," + lowNinePlusZero(min_2_index) + "," + lowNinePlusZero(min_3_index)+"</b></font>| 篮球 <font color=\"#56abe4\"><b>"+lowNinePlusZero(min_1_blue_index)+"</b></font>"));
        cold_100.setText(Html.fromHtml("冷号：红球 <font color=\"#eb4f38\"> <b>" + lowNinePlusZero(max_1_index) + "," + lowNinePlusZero(max_2_index) + "," + lowNinePlusZero(max_3_index)+"</b></font>| 篮球 <font color=\"#56abe4\"> <b>"+lowNinePlusZero(max_1_blue_index)+"</b></font>"));

        coldhotCompute(30);
        hot_30.setText(Html.fromHtml("热号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero(min_1_index) + "," +lowNinePlusZero(min_2_index ) + "," +lowNinePlusZero(min_3_index) +"</b></font>| 篮球 <font color=\"#56abe4\"> <b>"+lowNinePlusZero(min_1_blue_index)+"</b></font>"));
        cold_30.setText(Html.fromHtml("冷号：红球 <font color=\"#eb4f38\"><b>" + lowNinePlusZero(max_1_index) + "," +  lowNinePlusZero(max_2_index) + "," +  lowNinePlusZero(max_3_index)+"</b></font>| 篮球 <font color=\"#56abe4\"><b> "+ lowNinePlusZero(max_1_blue_index)+"</b></font>"));

        coldhotCompute(50);
        hot_50.setText(Html.fromHtml("热号：红球 <font color=\"#eb4f38\"><b>" +  lowNinePlusZero(min_1_index )+ "," + lowNinePlusZero( min_2_index) + "," +  lowNinePlusZero(min_3_index)+"</b></font>| 篮球 <font color=\"#56abe4\"> <b>"+ lowNinePlusZero(min_1_blue_index)+"</b></font>"));
        cold_50.setText(Html.fromHtml("冷号：红球 <font color=\"#eb4f38\"><b>" +  lowNinePlusZero(max_1_index) + "," +  lowNinePlusZero(max_2_index) + "," +  lowNinePlusZero(max_3_index)+"</b></font>| 篮球 <font color=\"#56abe4\"> <b>"+ lowNinePlusZero(max_1_blue_index)+"</b></font>"));

    }


    private String lowNinePlusZero(int z){
        if(z<10){
            return "0"+z;
        }else{
            return String.valueOf(z);
        }
    }

    @Override
    protected void onFailure(int requestCode) {

    }


    private void requestChart() {
        RequestParams params = new RequestParams(UrlConstants.TEST);
        params.addBodyParameter("lotteryType", "1001");
        params.addBodyParameter("page", "1");
        params.addBodyParameter("count", "100");
        httpPost(CommandUtil.TREND_CHART, params, false);

    }


    int min_1_index = 0;
    int min_2_index = 0;
    int min_3_index = 0;
    int max_1_index = 0;
    int max_2_index = 0;
    int max_3_index = 0;

    int min_1_blue_index = 0;
    int min_2_blue_index = 0;
    int max_1_blue_index = 0;
    int max_2_blue_index = 0;

    private void coldhotCompute(int start) {
        if (arrayChartItems == null) {
            return;
        }

        int[] values_100_red = new int[33];
        int[] values_100_blue = new int[16];

        for (int i = start; i < arrayChartItems.size(); i++) {
            SSQTrendChart chart = arrayChartItems.get(i);
            String[] hongMiss = chart.hongMiss();
            String[] lanMiss = chart.lanMiss();
            for (int j = 0; j < hongMiss.length; j++) {
                values_100_red[j] += Integer.parseInt(hongMiss[j]);
            }
            for (int k = 0; k < lanMiss.length; k++) {
                values_100_blue[k] += Integer.parseInt(lanMiss[k]);
            }
        }


        ArrayList<Integer> maps = new ArrayList<>();
        for (int i = 0; i < values_100_red.length; i++) {
            maps.add(values_100_red[i]);
        }
        ArrayList<Integer> maps_blue = new ArrayList<>();
        for (int i = 0; i < values_100_blue.length; i++) {
            maps_blue.add(values_100_blue[i]);
        }
        Arrays.sort(values_100_red);
        Arrays.sort(values_100_blue);


        int min_1_blue = values_100_blue[0];
        int min_2_blue = values_100_blue[0];

        int max_1_blue = values_100_blue[15];
        int max_2_blue = values_100_blue[14];

        int min_1 = values_100_red[0];
        int min_2 = values_100_red[1];
        int min_3 = values_100_red[2];

//        x.Ext.setDebug(true);

        LogUtil.v("冷号：" + min_1 + "," + min_2 + "," + min_3);

        int max_1 = values_100_red[32];
        int max_2 = values_100_red[31];
        int max_3 = values_100_red[30];
        LogUtil.v("热号：" + max_1 + "," + max_2 + "," + max_3);


        for (int i = 0; i < maps.size(); i++) {

            if (maps.get(i) == min_1) {
                min_1_index = i + 1;
            }
            if (maps.get(i) == min_2) {
                min_2_index = i + 1;
            }
            if (maps.get(i) == min_3) {
                min_3_index = i + 1;
            }
            if (maps.get(i) == max_1) {
                max_1_index = i + 1;
            }
            if (maps.get(i) == max_2) {
                max_2_index = i + 1;
            }
            if (maps.get(i) == max_3) {
                max_3_index = i + 1;
            }
        }
        for (int i = 0; i < maps_blue.size(); i++) {

            if (maps_blue.get(i) == min_1_blue) {
                min_1_blue_index = i + 1;
            }
            if (maps_blue.get(i) == min_2_blue) {
                min_2_blue_index = i + 1;
            }
            if (maps_blue.get(i) == max_1_blue) {
                max_1_blue_index = i + 1;
            }
            if (maps_blue.get(i) == max_2_blue) {
                max_2_blue_index = i + 1;
            }
        }


    }


}
